package com.linyubing.servlet;

import com.alibaba.fastjson.JSON;
import com.linyubing.dao.BaseDao;
import com.linyubing.dao.BaseDaoImpl;
import com.linyubing.pojo.Course;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/course/*")
public class CourseServlet extends BaseServlet {
    //创建BaseDaoImpl对象
    BaseDao<Course> dao = new BaseDaoImpl<>(Course.class);
    public void getAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //查询所有的课程
        List<Course> courseList = dao.getAll();
        if(courseList!=null&&courseList.size()!=0){
            //转换为json
            String jsonString = JSON.toJSONString(courseList);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }

    }
    public void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取请求参数
        int courseId= Integer.parseInt(req.getParameter("courseId"));
        //调用数据库的根据id查询数据
        Course course = dao.getById(courseId);
        if(course!=null){
            //转换为json
            String jsonString = JSON.toJSONString(course);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //post请求的乱码问题
        req.setCharacterEncoding("utf-8");
        //获取请求参数
        String courseName = req.getParameter("courseName");
        int courseTime = Integer.parseInt(req.getParameter("courseTime"));
        //调用数据库的新增方法
        Course course = new Course();
        course.setCourseName(courseName);
        course.setCourseTime(courseTime);
        boolean flag = dao.add(course);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException, InvocationTargetException, IllegalAccessException {
        //post请求的乱码问题
        req.setCharacterEncoding("utf-8");
        //获取请求参数
        int courseId = Integer.parseInt(req.getParameter("courseId"));
        String courseName = req.getParameter("courseName");
        int courseTime = Integer.parseInt(req.getParameter("courseTime"));
        //调用数据库的更新方法
        Course course = new Course(courseId, courseName, courseTime);
        boolean flag = dao.update(course);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        //获取请求参数
        int courseId= Integer.parseInt(req.getParameter("courseId"));
        //调用数据库的删除方法
        boolean flag = dao.deleteById(courseId);
        if(flag){
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("success");
        }
    }
}
